<template>
    <el-dialog ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="50%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="标题" prop="title">
                <el-input v-model="ruleForm.title" class="w100p" clearable placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="跳转类型" prop="jumpType">
                <el-select v-model="ruleForm.jumpType" class="w100p" clearable filterable placeholder="请选择">
                    <el-option v-for="item, index in enumListJumpType" :key="index" :label="item.text"
                        :value="item.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="跳转链接" v-if="ruleForm.jumpType == 'URL'" prop="url">
                <el-input v-model="ruleForm.url" class="w100p" clearable placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="选择文章" v-if="ruleForm.jumpType == 'ARTICLE'" prop="articleId">
                <el-select v-model="ruleForm.articleId" class="w100p" clearable filterable placeholder="请选择">
                    <el-option v-for="item, index in articleList" :key="index" :label="item.title"
                        :value="item.articleId"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="显示/隐藏" prop="status">
                <el-select v-model="ruleForm.status" class="w100p" clearable filterable placeholder="请选择">
                    <el-option label="显示" :value="true"></el-option>
                    <el-option label="隐藏" :value="false"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="轮播图板块" prop="carouselChartTypeId">
                <el-select v-model="ruleForm.carouselChartTypeId" class="w100p" clearable filterable placeholder="请选择">
                    <el-option v-for="item, index in enumListCarouselChartType" :key="index"
                        :label="item.carouselChartTypeName" :value="item.carouselChartTypeId"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="轮播图" prop="filesId">
                <UpLoadImage v-if="dialogVisible" @getFileIdList="getFileIdList" :idList="fileIdList" :limit="1">
                </UpLoadImage>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import UpLoadImage from "@/components/UpLoadImage";
import carouselChart from "@/api/carouselChart"
export default {
    components: { UpLoadImage },
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        articleList: {
            type: Array,
            default: () => {
                return []
            }
        },
        enumListCarouselChartType: {
            type: Array,
            default: () => {
                return []
            }
        },
        enumListJumpType: {
            type: Array,
            default: () => {
                return []
            }
        },
    },
    data() {
        // 自定义验证规则，检查网址是否为有效的URL
        var validateUrl = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('网址不能为空'));
            }
            // 正则表达式判断网址格式
            const reg = /(http|https):\/\/([\w.]+\/?)\S*/
            if (reg.test(value)) {
                callback();
            } else {
                callback('请输入正确的网址');
            }
        };
        return {
            fileIdList: [],
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            rules: {
                title: [
                    { required: true, message: '请输入', trigger: 'change' }
                ],
                url: [
                    { required: true, message: '请输入', trigger: 'change' },
                    { validator: validateUrl, trigger: 'blur' }
                ],
                carouselChartType: [
                    { required: true, message: '请选择', trigger: 'change' }
                ],
                articleId: [
                    { required: true, message: '请选择', trigger: 'change' }
                ],
                filesId: [
                    { required: true, message: '请上传图片', trigger: 'change' }
                ],
                status: [
                    { required: true, message: '请选择', trigger: 'change' }
                ],
                jumpType: [
                    { required: true, message: '请选择', trigger: 'change' }
                ],
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },

    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm();
                this.$set(this.ruleForm, 'status', true);
                this.addEditApi = carouselChart.add;
                this.addEditName = '添加';
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data };
                this.fileIdList = this.data.filesId ? [this.data.filesId] : [];
                this.addEditApi = carouselChart.edit;
                this.addEditName = '编辑';
            }
        },
        getFileIdList(data) {
            let id = data.fileIdList && data.fileIdList.length > 0 ? data.fileIdList[0] : ''
            this.$set(this.ruleForm, 'filesId', id)
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({ ...this.ruleForm }).then(res => {
                        let success = res.code == 0
                        let msg = success ? '操作成功' : '操作失败'
                        this.$message[success ? 'success' : 'warning'](res.msg || msg)
                        success && this.cancel();
                    }).catch(err => {
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.fileIdList = [];
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped></style>